package cn.zxone.feishu.demo.common.enums;


import cn.zxone.feishu.demo.common.pojo.IResultCode;

/**
 * <p>
 *     接口响应状态枚举实现
 *     0-成功
 *     1-失败
 *
 *	   保留号段：10000～19999
 *
 *	   10000-参数错误
 *	   10001-业务处理错误
 *	   10002-自定义异常错误
 *
 *
 *
 * </p>
 * @date 2023-07-27 17:43:15
 */
public enum ResultCode implements IResultCode {

	SUCCESS(0, "成功"),
	FAILED(1, "失败"),

	BAD_REQUEST(400, "请求参数不正确"),
	UNAUTHORIZED(401, "未授权"),
	FORBIDDEN(403, "没有该操作权限"),
	NOT_FOUND(404, "服务未找到"),
	METHOD_NOT_ALLOWED(405, "请求方法不正确"),
	REQUEST_TIME_OUT(408, "请求超时"),
	LOCKED(423, "请求失败，请稍后重试"),
	TOO_MANY_REQUESTS(429, "请求过于频繁，请稍后重试"),

	/**
	 * 系统相关错误：500～599
	 */
	ERROR(500, "系统错误"),
	BAD_GATEWAY(502, "网关错误"),
	SERVICE_UNAVAILABLE(503, "服务不可用"),


	PARAM_ERROR(10000, "参数错误"),
	BUSINESS_ERROR(10001, "业务处理错误"),
	CUSTOM_ERROR(10002, "错误"),


	// 其它错误：11000～99999




	;

	/**
	 * 状态码
	 */
	final int code;

	/**
	 * 消息内容
	 */
	final String message;

	ResultCode(int code, String message) {
		this.code = code;
		this.message = message;
	}

	@Override
	public int getCode() {
		return code;
	}

	@Override
	public String getMessage() {
		return message;
	}

	public static ResultCode getEnum(Integer code){

		if(null == code){
			return null;
		}

		for (ResultCode resEnum : ResultCode.values()) {
			if (resEnum.getCode() == code.intValue()) {
				return resEnum;
			}
		}
		return null;
	}

}
